package com.hymane.etvexample;

import com.hymane.etvexample.slice.InputDialog;
import com.hymane.expandtextview.ExpandTextView;
import com.hymane.expandtextview.OnReadMoreClickListener;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.window.service.WindowManager;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class MainAbility extends Ability implements Component.ClickedListener  {
    private ExpandTextView etv;
    private Text text;
    private Text test;
    private boolean img;
    private int height;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(new RgbColor(48, 63, 159).asArgbInt());
        super.setUIContent(ResourceTable.Layout_ability_main);
        etv = (ExpandTextView) findComponentById(ResourceTable.Id_etv);
        Slider sb_duration = (Slider) findComponentById(ResourceTable.Id_sb_duration);
        Button btn_modify_content = (Button) findComponentById(ResourceTable.Id_btn_modify_content);
        Button    btn_modify_icon = (Button) findComponentById(ResourceTable.Id_btn_modify_icon);
        Slider   sb_min_line = (Slider) findComponentById(ResourceTable.Id_sb_min_line);
        btn_modify_content.setClickedListener(this);
        btn_modify_icon.setClickedListener(this);
        text = (Text) findComponentById(ResourceTable.Id_text);
        test = (Text) findComponentById(ResourceTable.Id_test);
        EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());
        etv.setContent("哈氨基酸等今晚没空去骂死你的奶第七名大门的情况我们的马克思的门口吗傻么的空间马赛克默多克马赛克的吗萨姆的卡茅塞顿开马上买点卡莫斯科的马上发年初您请问你地加上就打卡开什么车市场价氨气味就代码可每次看目前我我解放你的是充满我看到静安寺的卡美森看代码哈撒打算的家具是大家就圣诞节氨基酸等今晚没空去骂死你的奶第七名大门的情况我们的马克思的门口吗傻么的空间马赛克默多克马赛克的吗萨姆的卡茅塞顿开马上买点卡莫斯科的马上发年初您请问你地加上就打卡开什么车市场价氨气味就代码可每次看目前我我解放你的是充满我看到静安寺的卡美森看代码哈撒打算的家具是大家就圣诞节氨基酸等今晚没空去骂死你的奶第七名大门的情况我们的马克思的门口吗傻么的空间马赛克默多克马赛克的吗萨姆的卡茅塞顿开马上买点卡莫斯科的马上发年初您请问你地加上就打卡开什么车市场价氨气味就代码可每次看目前我我解放你的是充满我看到静安寺的卡美森看代码哈撒打算的家具是大家就圣诞节氨基酸等今晚没空去骂死你的奶第七名大门的情况我们的马克思的门口吗傻么的空间马赛克默多克马赛克的吗萨姆的卡茅塞顿开马上买点卡莫斯科的马上发年初您请问你地加上就打卡开什么车市场价氨气味就代码可每次看目前我我解放你的是充满我看到静安寺的卡美森看代码");
        etv.setOnReadMoreListener(new OnReadMoreClickListener() {
            @Override
            public void onExpand() {
                text.setVisibility(Component.VISIBLE);
                text.setText("expand");
                handler.postTask(() -> text.setVisibility(Component.HIDE), 1500);
            }

            @Override
            public void onFold() {
                text.setText("fold");
                text.setVisibility(Component.VISIBLE);
                handler.postTask(() -> text.setVisibility(Component.HIDE), 1500);
            }
        });
        sb_duration.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                etv.setAnimationDuration(slider.getProgress());
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        sb_min_line.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                etv.setMinVisibleLines(slider.getProgress());
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });

    }
    @Override
    public void onActive() {
        super.onActive();
        String str = etv.getContent();
        test.setText(str);
        test.addDrawTask((component1, canvas) -> {
            height=test.getHeight();
            etv.setMaxHeight(height);
            etv.saveHeight(height);
            test.setText("");
        });
    }
    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_btn_modify_content) {
            InputDialog inputDialog = new InputDialog(this);
            inputDialog.setListener(new InputDialog.Listener() {
                @Override
                public void getLeftListener() {
                    inputDialog.destroy();
                }

                @Override
                public void getRightListener() {
                    String str = inputDialog.getContent();

                    test.setText(str);
                    test.addDrawTask((component1, canvas) -> {
                        height=test.getHeight();
                        etv.setMaxHeight(height);
                        etv.saveHeight(height);
                        test.setText("");
                    });

                    etv.setContent(str);
                    inputDialog.destroy();
                }
            });
            inputDialog.setDialogListener(() -> {
                inputDialog.destroy();
                return false;
            });
            inputDialog.show();
        } else if (component.getId() == ResourceTable.Id_btn_modify_icon) {
            if (!img) {

                etv.setIndicateImage(ResourceTable.Media_ic_keyboard_arrow_down_black_24dp);
                img = true;
            } else {
                img = false;
                etv.setIndicateImage(ResourceTable.Media_ic_arrow_down_light_round);
            }

        }

    }
}
